﻿#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

#include "list.h"


void test_list1()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);

	//遍历方式1
	list<int>::iterator it = lt.begin();
	while (it != lt.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

	//遍历方式2
	//g++ test_list1.cpp -std=C++11
	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;

	int a[] = { 16,2,77,29 };
	list<int> lt1(a, a + 4);
	for (auto e : lt1)
	{
		cout << e << " ";
	}
	cout << endl;

	vector<int> v(a, a + 4);
	sort(v.begin(), v.end());
	//sort的底层使用的是快速排序，快速排序要求容器迭代器必须是随机迭代器
	//比如：快排要三数取中优化，不支持随机访问，效率就不够了

	
}
template<class Con>
void PrintContainer(const Con& c)
{
	typename Con::const_iterator it = c.begin();
	while (it!= c.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
}

void test_list2()
{
	list<int> lt;
	lt.push_back(1);
	lt.push_back(2);
	lt.push_back(3);
	lt.push_back(4);

	PrintContainer(lt);

	list<int>::iterator pos = find(lt.begin(), lt.end(), 3);
	lt.insert(pos, 30);
	PrintContainer(lt);

	cout << *pos << endl;

	//erase之后，迭代器会失效，pos成为野指针
	lt.erase(pos);
	PrintContainer(lt);

	cout << *pos << endl;


}
int main()
{
	//test_list1();
	//test_list2();
	ly::test_list1();
	return 0;
}